package com.lagou.server;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class StaticResourceUtil {

    //获取静态资源绝对路径
    public  static String getAbsolutePath(String path){

        String bsolutePath=StaticResourceUtil.class.getResource("/").getPath();
        return  bsolutePath.replaceAll("\\\\","/")+path;
    }

    public static void outputStaticResource(InputStream inputStream, OutputStream outputStream) throws IOException {

        int count =0;
        while (count==0){
             count = inputStream.available();
        }
        int resourceSize=count;
        //输出http请求头  内容稍后输出
        outputStream.write(HttpProtocoUtil.getHttpHeader200(resourceSize).getBytes());

        long written=0;
        int byteSize=1024;
        byte[] bytes=new byte[byteSize];
        while (written<resourceSize){
                 if(written +byteSize>resourceSize){//说明剩余数据大小不足1024 输出真实大小
                     byteSize= (int) (resourceSize-written);
                     bytes=new byte[byteSize];
                 }
                 inputStream.read(bytes);
                 outputStream.write(bytes);
                 outputStream.flush();
                 written+=byteSize;
        }
    }


}
